#include "Detection.h"
#include <iostream>
#include <QtNetwork/QtNetwork>
#include <QtGui>

using namespace std;


Detection::Detection(){


    int port = 10002;
    string net_ref_address="224.5.23.2";
    string net_ref_interface="";

    //server = new QUdpSocket();
    //Net::Address multiaddr,interface;
    //multiaddr.setHost(net_ref_address.c_str(),port);
    //interface.setAny();


    /*
    if(!MultiCastsocket.addMulticast(multiaddr,interface)) {
        cout << "error add multicast" << endl;
    }
    cout << "configured" << endl;
    */

}

Detection::Detection(int port = 10002,
                     string net_ref_address="224.5.23.2",
                     string net_ref_interface=""){




    Net::Address multiaddr,interface;
    multiaddr.setHost(net_ref_address.c_str(),port);
    interface.setAny();


    if(!MultiCastsocket.addMulticast(multiaddr,interface)) {
        cout << "error add multicast" << endl;
    }
    cout << "configured" << endl;


}
Detection::~Detection(){

}
bool Detection::open(){

    if(!MultiCastsocket.open(port,true,true)) {
        cout << "open fail"<<endl;
        return(false);
    }



    return(true);
  }

void Detection::recv(){

    //QHostAddress sender;
    quint16 port2;
    grSim_Packet packet;

    char data[1000];
    cout << "recv" << endl;
    //int size = commandSocket->readDatagram(data, 65536, &sender, &port2);

    //int size = MultiCastsocket.recv(data,1000,_addr);

    //cout << "datasize: "; cout<< size << endl;
    Efrom.ParseFromArray(data,1000);


}
bool Detection::isopen(){
    return MultiCastsocket.isOpen();
}
